package Test_2019_2_26.Test3.dao;

import org.springframework.jdbc.core.support.JdbcDaoSupport;

/**
 * @Description:转账案例的DAO层实现类
 */
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
    // 是否低于0
    private boolean isReally = true;
    /**
     * @param out   :转出账号
     * @param money :转账金额
     */
    @Override
    public void outMoney(String out, Double money) {
        String sql = "update account set money = money-? where name = ?";
        this.getJdbcTemplate().update(sql, money, out);

        String sql2 = "select money from account where name = '" + out + "'";
        String isMoney = this.getJdbcTemplate().queryForObject(sql2, String.class);
        if( Double.parseDouble(isMoney) < 0){
            String sql3 = "update account set money = money+? where name = ?";
            this.getJdbcTemplate().update(sql3, money, out);
            System.out.println("余额不足！");
            // 是否低于0
            isReally = false;
        }else{
            System.out.println("转出成功！");
        }
    }

    /**
     * @param in    :转入账号
     * @param money :转账金额
     */
    @Override
    public void inMoney(String in, Double money) {
        if(isReally){
            String sql = "update account set money = money+? where name = ?";
            this.getJdbcTemplate().update(sql, money, in);
            System.out.println("转入成功！");
        }else{
            System.out.println("转入失败！");
        }

    }

}
